<?php
include_once "settings.php";
include_once "lang.php";


echo <<<HTML_HEADER
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>IBDB - Search</title>

<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<link rel="stylesheet" type="text/css" href="main.css">

</head>
<body>
HTML_HEADER;




function printCurrentBook($currentBook,$bookNumber)
{

	global $langList;
	global $COVER_DIR;
	global $BOOK_DIR;


	echo "<span class='bookNumber'>" . ($bookNumber + 1) . "</span>";

	echo "<li class='bookListLI'>" ;

	echo "<img class='coverImg' width='90' src=\"".$COVER_DIR.$currentBook["coverFileName"][0]. "\">";

		echo "<div class='bookBriefInfo'>";

			if (isset($currentBook["sequenceFull"]))
			{
				echo "<p><a href='fullView.php?bookID=".$currentBook["bookID"][0]."'>". $currentBook["title"][0] . " </a>";
				foreach ($currentBook["sequenceFull"] as $currentSequence)
				{
					$sequenceTmp = explode("#", $currentSequence);
					$currentBook["sequenceName"][0] = $sequenceTmp[0] ;
					$currentBook["sequenceNumber"][0] = $sequenceTmp[1] ;
$seq = "<a href='index.php?searchTerm=" . $_GET['searchTerm'] . getFQparameters() . "&fq=sequenceFull_zzz_s:\"".addslashes($currentBook["sequenceName"][0])."\"&start=0&rows=10'>" .$currentBook["sequenceName"][0]. "</a>" ;

echo " Seq: (" . $seq ." - ". $currentBook["sequenceNumber"][0] . ") </a>";
				}
				echo "</p>";
			}
			else
			{
				echo "<p><a href='fullView.php?bookID=".$currentBook["bookID"][0]."'>". $currentBook["title"][0]. "</a></p>";
			}


			echo "<p class='authorBrief'>" ;
			foreach ($currentBook["authorFullName"] as $currentAuthorName)
			{
$authorLink = "<a href='index.php?searchTerm=" . $_GET['searchTerm'] . getFQparameters() . "&fq=authorFullName:\"".addslashes($currentAuthorName).
"\"&start=0&rows=10'>" .$currentAuthorName. "</a>" ;
				echo $authorLink . "<br>" ;
			}
			echo "</p>";


			if (isset($currentBook["annotation"]))
			{
				if (mb_strlen($currentBook["annotation"][0]) > 500)
				{
					echo "<p class='annotation'>". mb_strcut($currentBook["annotation"][0],0,500,"UTF-8") . "...</p>";
				}
				else
				{
					echo "<p>" .  $currentBook["annotation"][0] . "</p>";
				}

			}



$linkToFile = "<a href=\"/" . $BOOK_DIR . mb_substr($currentBook["fileName"][0],0,2)."/".$currentBook["fileName"][0] . "\">" . $currentBook["fileName"][0] . "</a>" ;
$linkToLang = "<a href='index.php?searchTerm=" . $_GET['searchTerm'] . getFQparameters()."&fq=lang:\"".addslashes($currentBook["lang"][0])."\"&start=0&rows=10'>" .$langList[$currentBook["lang"][0]].  "</a>" ;

			$technicalDetails =
				"<strong>File name: </strong>".$linkToFile .
				"<strong> File type: </strong>".$currentBook["fileType"][0] .
				"<strong> File size: </strong>". round($currentBook["fileSize"][0]/1024) . "Kb" .
				"<strong> Language: </strong>".$linkToLang ;



			echo "<p>" .$technicalDetails. "</p>";
		echo "</div>";
	echo "</li>" ;
}


	if (!$_GET)
	{
		printSearchBox();
	}
	else
	{

		printSearchBox();


//$_GET["searchTerm"]
//$_GET["start"]
//$_GET["rows"]
//$_GET["fq"]



		$searchTerm = rawurlencode($_GET["searchTerm"]);
		$additionalQuery = getFQparameters();

		$code = file_get_contents("http://localhost:8983/solr/collection1/select?q=".$searchTerm.
																		   "&start=".$_GET["start"].
																		   "&rows=".$_GET["rows"].
																		    $additionalQuery.
																			"&wt=php&indent=true&facet=true&facet.field=sequenceFull_zzz_s&facet.field=lang_s&facet.field=authorFullName_s");
		eval("\$result = " . $code . ";");

		$numberOfResults = $result["response"]["numFound"] ;

		echo "<div class='searchRefines'><ul>" ;

		echo "<li><b>Searched for: </b>".$result["responseHeader"]["params"]["q"] ."</li>";
		echo "<li><b>Got results: </b>".$numberOfResults ."</li>";
		echo "</ul>";
		echo "<br><b>Refine: </b>".rawurldecode(getFQparameters());
		echo "</div>" ;


		echo "<div class='mainContainer'>" ;

		echo "<div class='facetsDiv'>";

		echo "<p class='facetTopic'>Authors<p>";

		foreach ($result['facet_counts']['facet_fields']['authorFullName_s'] as $key => $value)
		{
			if ($value > 0)
			{

$linkToAuthor = "<a href='index.php?searchTerm=" . $_GET['searchTerm'] . getFQparameters() . "&fq=authorFullName_s:\"".addslashes($key)."\"&start=0&rows=10'>" .$key. "</a>" ;

				echo "<p class='facetList'>" . $linkToAuthor . "</p><p class='facetCounter'>" . $value . "</p>";
			}
		}

		echo "<p class='facetTopic'>Sequence<p>";

		foreach ($result['facet_counts']['facet_fields']['sequenceFull_zzz_s'] as $key => $value)
		{
			if (is_int($key)) { continue ; }

			if ($value >= 1)
			{
$linkToSeq = "<a href='index.php?searchTerm=" . $_GET['searchTerm'] . getFQparameters()."&fq=sequenceFull_zzz_s:\"".addslashes($key)."\"&start=0&rows=10'>" .$key.  "</a>" ;
				echo "<p class='facetList'>" . $linkToSeq . "</p><p class='facetCounter'>" . $value . "</p>";
			}
		}

		echo "<p class='facetTopic'>Language<p>";

		foreach ($result['facet_counts']['facet_fields']['lang_s'] as $key => $value)
		{
			if ($value > 0)
			{
$linkToLang = "<a href='index.php?searchTerm=" . $_GET['searchTerm'] . getFQparameters()."&fq=lang:\"".addslashes($key)."\"&start=0&rows=10'>" .$langList[$key].  "</a>" ;
				echo "<p class='facetList'>" . $linkToLang . "</p><p class='facetCounter'>" . $value . "</p>";
			}


		}

		echo "</div>";


		echo "<div class='mainBookList'>";
		echo "<ul class='mainBookList'>";
		$bookNumber=$_GET["start"];

		foreach ($result["response"]["docs"] as $currentBook)
		{

			printCurrentBook($currentBook,$bookNumber);
			$bookNumber = $bookNumber + 1 ;
		}
		echo "</ul>";

		for ($counter=1; $counter <= ceil($numberOfResults / 10) ; $counter++)
		{
			if (ceil(($_GET["start"] / 10)+1) == $counter)
			{
				echo "<b>" . "<a href='index.php?searchTerm=". $_GET["searchTerm"] . "&start=" . (($counter - 1) * 10) . "&rows=10".$additionalQuery."&wt=php&indent=true'>".$counter. "</a>" . "</b> ";
			}
			else
			{
				echo "<a href='index.php?searchTerm=". $_GET["searchTerm"] . "&start=" . (($counter - 1) * 10) . "&rows=10".$additionalQuery."&wt=php&indent=true'>" . $counter . "</a>" . " ";
			}


		}
echo "</div></div>" ;

	}

echo <<<HTML_FOOTER
</body>
</html>
HTML_FOOTER;

?>
